package dkr.start;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.jsp.tagext.TagInfo;

import org.apache.jasper.EmbeddedServletOptions;
import org.apache.jasper.JspC;
import org.apache.jasper.Options;
import org.apache.jasper.compiler.JspRuntimeContext;
import org.apache.jasper.servlet.JspCServletContext;
import org.apache.jasper.servlet.JspServlet;
import org.apache.jasper.servlet.JspServletWrapper;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

import com.mongodb.DBAddress;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;


public class Main {
	public static Logger getFileLogger(String name, String path, Level level) throws IOException{
		PatternLayout layout = new PatternLayout("[%d{yyyy/MM/dd HH:mm:ss.SSS}][%5p]:%m%n");
		
		Logger.getRootLogger().setLevel(level);
		DailyRollingFileAppender drfa = new DailyRollingFileAppender(layout, path + ".out", "yyyy-MM-dd");
		drfa.setBufferSize(1024);
		drfa.setEncoding("UTF-8");
		drfa.setImmediateFlush(true);
		Logger.getRootLogger().addAppender(drfa);
		
		Logger log = Logger.getLogger("dkr");
		RollingFileAppender rfa = new RollingFileAppender(layout, path, true);
		rfa.setImmediateFlush(true);
		rfa.setEncoding("UTF-8");
		rfa.setMaxFileSize("5MB");
		log.addAppender(rfa);
		
		ConsoleAppender ca = new ConsoleAppender(layout);
		ca.setEncoding("UTF-8");
		log.addAppender(ca);
		log.setLevel(level);
		Logger.getRootLogger().addAppender(ca);
		return log;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			File f = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
			String dkrHome = f.getParentFile().getPath();
			Logger log = getFileLogger("start", dkrHome + File.separator + "logs" + File.separator + "start.log", Level.INFO);
			log.info("=====start======");
			log.info("DKR_HOME: " + dkrHome);
			if(args != null && args.length > 0){
				log.info("args:");
				for(String arg : args){
					log.info("\t" + arg);
				}
			}
			
//			MongoClient client = new MongoClient();
//			DBAddress addr = new DBAddress(host, port, dbname)
			
			QueuedThreadPool threadPool = new QueuedThreadPool(100, 10);
			Server server = new Server(threadPool);
			ServerConnector connector = new ServerConnector(server);
			connector.setPort(8080);
			server.addConnector(connector);
			
			MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
	        server.addBean(mbContainer);
	        
	        ManagementFactory.getOperatingSystemMXBean();
	        
	        HandlerList handlerList = new HandlerList();
	        
	        ResourceHandler resourceHandler = new ResourceHandler();
	        resourceHandler.setResourceBase(dkrHome + File.separator + "static");
	        resourceHandler.setDirectoriesListed(true);
	        
	        ContextHandler staticHandler = new ContextHandler("/static");
	        staticHandler.setHandler(resourceHandler);
	        handlerList.addHandler(staticHandler);
			
//			ServletContextHandler servletHandler = new ServletContextHandler(server, "/dre");
//			TestServlet.DKR_HOME = dkrHome;
//			servletHandler.addServlet(TestServlet.class, "/");
//			handlerList.addHandler(servletHandler);
			
			WebAppContext webapp = new WebAppContext(dkrHome + File.separator + "test", "/test");
//			webapp.setContextPath("/test");
//			webapp.setWar(dkrHome + File.separator + "test.war");
			handlerList.addHandler(webapp);

			server.setHandler(handlerList);
			
			server.start();
			server.join();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}

}
